import wx
import numpy
from wx.glcanvas import GLCanvas
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import render

class Triangle:
    pass

class MyGLCanvas(GLCanvas):
    
    def __init__(self, parent, id):
        GLCanvas.__init__(self, parent, id)
        self._init = 0
        self._renderer = None
        self._EnableEvents()
        

    def _EnableEvents(self):
        wx.EVT_PAINT(self, self.OnPaint)
        wx.EVT_KEY_DOWN(self, self.Key)

    def OnPaint(self,event):
        wx.PaintDC(self)
        self.SetCurrent()
        if not self._init:
            self._renderer = render.Renderer(self.GetSize())
            self._renderer.Register(Triangle, render.TriangleRenderer())
            self._init = 1
        self.OnDraw()
        
    def Key(self,event):
        key = event.GetKeyCode()
        if key == 83:
            self._renderer.Camera.Rotate(0, 1, 0)
        elif key == 87:
            self._renderer.Camera.Rotate(0, -1, 0)
        elif key == 65:
            self._renderer.Camera.Move((-1, 0, 0))
        elif key == 68:
            self._renderer.Camera.Move((1, 0, 0))
        elif key == 81:
            self._renderer.Camera.Rotate(-1, 0, 0)
            pass
        elif key == 69:
            self._renderer.Camera.Rotate(1, 0, 0)
            pass
        event.Skip()
        self.Refresh()

    def OnDraw(self):
        t = Triangle()
        self._renderer.Render(t)
        self.SwapBuffers()

class GLDialog(wx.Dialog):
    def __init__(self, *args, **kwds):
        # begin wxGlade: GLDialog.__init__
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.gl_canvas = MyGLCanvas(self, -1)

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: GLDialog.__set_properties
        self.SetTitle("dialog_1")
        self.SetSize((400, 400))
        wx.EVT_CLOSE(self, self._OnClose)
        #self.gl_canvas.enableEvents()
        # end wxGlade

    def _OnClose(self, event):
        wx.Exit()

    def __do_layout(self):
        # begin wxGlade: GLDialog.__do_layout
        sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_1.Add(self.gl_canvas, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        self.Layout()
        # end wxGlade

# end of class GLDialog


if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    gl_dialog = GLDialog(None, -1, "")
    app.SetTopWindow(gl_dialog)
    gl_dialog.Show()
    app.MainLoop()